package com.hkbigdata.sybx.controller;

/**
 * @author yangjing
 * @creat 2025-05-30-10:28
 * @email 3010335359@qq.com
 */

import com.hkbigdata.sybx.bean.UserInterest;
import com.hkbigdata.sybx.mapper.UserInterestMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户兴趣选择控制器
 */
@RestController
@RequestMapping("/api/userInterest")
public class UserInterestController {

    @Autowired
    private UserInterestMapper userInterestMapper;

    /**
     * 提交用户兴趣选择
     * @param userInterest 用户兴趣实体
     * @return 操作结果
     */
    @PostMapping("/insert")
    public Map<String, Object> submitUserInterest(@RequestBody UserInterest userInterest) {
        Map<String, Object> result = new HashMap<>();

        try {
            int count = userInterestMapper.insertUserInterest(userInterest);
            if (count > 0) {
                result.put("code", 0);
                result.put("message", "兴趣选择提交成功");
                result.put("success", true);
                result.put("data", userInterest);
            } else {
                result.put("code", 1);
                result.put("message", "兴趣选择提交失败");
                result.put("success", false);
            }
        } catch (Exception e) {
            result.put("code", 1);
            result.put("message", "提交失败：" + e.getMessage());
            result.put("success", false);
            e.printStackTrace();
        }

        return result;
    }

    /**
     * 获取所有用户兴趣选择
     * @return 用户兴趣列表
     */
    @GetMapping("/all")
    public Map<String, Object> getAllUserInterests() {
        Map<String, Object> result = new HashMap<>();

        try {
            List<UserInterest> interests = userInterestMapper.getAllUserInterests();
            result.put("code", 0);
            result.put("message", "获取成功");
            result.put("success", true);
            result.put("data", interests);
        } catch (Exception e) {
            result.put("code", 1);
            result.put("message", "获取失败：" + e.getMessage());
            result.put("success", false);
            e.printStackTrace();
        }

        return result;
    }

    /**
     * 根据用户ID获取兴趣选择
     * @param userId 用户ID
     * @return 用户兴趣实体
     */
    @GetMapping("/{userId}")
    public Map<String, Object> getUserInterestByUserId(@PathVariable String userId) {
        Map<String, Object> result = new HashMap<>();

        try {
            UserInterest interest = userInterestMapper.getUserInterestByUserId(userId);
            if (interest != null) {
                result.put("code", 0);
                result.put("message", "获取成功");
                result.put("success", true);
                result.put("data", interest);
            } else {
                result.put("code", 1);
                result.put("message", "未找到用户兴趣数据");
                result.put("success", false);
            }
        } catch (Exception e) {
            result.put("code", 1);
            result.put("message", "获取失败：" + e.getMessage());
            result.put("success", false);
            e.printStackTrace();
        }

        return result;
    }
}